from django.shortcuts import render
from django.views import View

from userapp.models import User
from .models import *
from django.http import HttpResponseRedirect
# Create your views here.
#热门产品
class GoodsListView(View):
    def get(self, request):
        user = request.session.get('user')
        # user = User.objects.get(id=user.id)
        #随机选取8个商品
        goods_list = Goods.objects.order_by('?')[:8]
        if user:
            user = User.objects.get(id=user.id)
            return render(request,'index.html',{'goods_list':goods_list,'user':user})
        else:
            return render(request,'index.html',{'goods_list':goods_list})



#店铺商品
class ShopView(View):
    def get(self,request,model_id=1):
        user = request.session.get('user')
        user = User.objects.get(id = user.id)
        model_list = Goodsmodel.objects.all().order_by('id')
        good_list = Goods.objects.filter(model_id=model_id)
        return render(request,'shop.html',{'model_list':model_list,'good_list':good_list,'model_id':model_id,'user':user})

    def post(self,request,model_id=1):
        quantity1 = request.POST['quantity1']
        print(quantity1)
        return HttpResponseRedirect('/')

#商品详情
class DtailView(View):
    def get(self,request,good_id):
        user = request.session.get('user')
        user = User.objects.get(id = user.id)
        good_list = Goods.objects.filter(id=good_id)
        return render(request,'detail.html',{'good_id':good_id,'good_list':good_list,'user':user})

# 搜索
class SearchView(View):
    def post(self,request):
        key = request.POST.get('key')
        print(key)
        search_list = Goods.objects.filter(goods_name__contains=key)

        return render(request, 'search.html', {'goods_list':search_list})






